package com.drug.mingkang.controller;

import com.drug.mingkang.model.HqInventory;
import com.drug.mingkang.model.HqWarehousing;
import com.drug.mingkang.model.ManufacturePlan;
import com.drug.mingkang.permission.model.PerUsers;
import com.drug.mingkang.service.HqInventoryService;
import com.drug.mingkang.service.HqWarehousingService;
import com.drug.mingkang.service.ManufacturePlanService;
import com.drug.mingkang.utils.ResponseMessage;
import com.drug.mingkang.utils.Tools;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 制造计划表(ManufacturePlan)表控制层
 *
 * @author xqj
 * @since 2020-08-27 09:12:03
 */
@RestController
@RequestMapping("manufacturePlan")
public class ManufacturePlanController {
    /**
     * 服务对象
     */
    @Resource
    private ManufacturePlanService manufacturePlanService;


    @Resource
    private HqInventoryService hqInventoryService;

    @Resource
    private HqWarehousingService warehousingService;


    @Resource
    private HttpSession session;



    /**
     * 根据计划单id查询数据
     * @param planId
     * @return
     */
    @ApiOperation(value = "根据计划单id查询数据 开始质检",notes = "传入计划单id",httpMethod = "GET",response = ResponseMessage.class)
    @GetMapping("queryByPlanId")
    public List<ManufacturePlan> queryByPlanId(String planId){

        List<ManufacturePlan> list = this.manufacturePlanService.getPlanId(planId);
        return list;
    }


    /**
     * 查询所有的数据并分页
     * @param manufacturePlan
     * @return
     */
    @ApiOperation(value = "查询所有的数据并分页",notes = "传入manufacturePlan对象",httpMethod = "GET",response = ResponseMessage.class)
    @GetMapping("findAll")
    public  Map<String,Object> findAll(ManufacturePlan manufacturePlan){
        List<ManufacturePlan> list = this.manufacturePlanService.findAll(manufacturePlan);
        int count = this.manufacturePlanService.getCount(manufacturePlan);
        return new Tools().limit(list,count);
    }

    /**
     * 开始制造
     *
     * @param planId 生产计划单id
     * @return 单条数据
     */
    @ApiOperation(value = "根据计划单id查询数据 开始制造",notes = "传入计划单id planId",httpMethod = "GET",response = ResponseMessage.class)
    @GetMapping("selectOne")
    public Map<String,Object> selectOne(String planId) {
        List<ManufacturePlan> list = this.manufacturePlanService.queryById(planId);
        return new Tools().limit(list,list.size());
    }


    /**
     * 质检入库
     * @param jsonStr
     * @param remark
     * @return
     */
    @ApiOperation(value = "质检入库",notes = "传入ManufacturePlan对象数组json字符串和remark",httpMethod = "POST",response =ResponseMessage.class)
    @PostMapping("quality")
    public ResponseMessage quality(String jsonStr,String remark){
        Timestamp timestamp = new Timestamp(new Date().getTime());
        List<ManufacturePlan> list = new Tools().jsonToList(jsonStr,ManufacturePlan.class);
        List<HqWarehousing> warehousingList = new ArrayList<>();

        ResponseMessage responseMessage = new ResponseMessage();
        //质检入库后增加入库明细
        int row = this.manufacturePlanService.quality(list,remark);
        PerUsers perUsers = (PerUsers) session.getAttribute("perUsers");
        if (row>0){

            for(ManufacturePlan manufacturePlan: list){
                HqInventory hqInventory =  this.hqInventoryService.queryByDrugId(manufacturePlan.getDrugId());
                HqWarehousing warehousing = new HqWarehousing();
                warehousing.setInventoryId(hqInventory.getInventoryId());
                warehousing.setOper(perUsers.getUserName());
                warehousing.setState(1);
                warehousing.setWareNum(manufacturePlan.getMpTask());
                warehousing.setWareTime(timestamp);
                warehousingList.add(warehousing);
            }
            responseMessage = this.warehousingService.insertBatch(warehousingList)>0?ResponseMessage.success():ResponseMessage.error();

        }




        return responseMessage;
    }


    /**
     * 制造
     * @param jsonStr
     * @return
     */
    @ApiOperation(value = "药品制造",notes = "传入ManufacturePlan对象数组json字符串",httpMethod = "POST",response = ResponseMessage.class)
    @PostMapping("madeDrug")
    public ResponseMessage madeDrug(String jsonStr){

        List<ManufacturePlan> list = new Tools().jsonToList(jsonStr,ManufacturePlan.class);


        ResponseMessage responseMessage = this.manufacturePlanService.updateBatch(list)>0?ResponseMessage.success():ResponseMessage.error();



        return responseMessage;
    }

}